package com.aibili.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.aibili.pojo.BlackList;
import com.aibili.pojo.ChargeRecord;
import com.aibili.pojo.ReportInfo;
import com.aibili.pojo.UserAttention;
import com.aibili.pojo.UserFavorability;
import com.aibili.pojo.UserInfo;
import com.aibili.pojo.model.PageBodyModel;
import com.aibili.pojo.model.PageModel;
import com.daoshun.common.CommonUtils;

@Service("chatService")
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class ChatService extends BaseService {

	/**
	 * 
	 * @Description 新增关注
	 * @param uatt
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void attention(UserAttention uatt) {
		dataDao.addObject(uatt);
	}

	/**
	 * 
	 * @Description 取消关注
	 * @param fid
	 * @param tid
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void cancel_attention(long fid, long tid) {
		String hql = " from UserAttention where from_id = :from_id and to_id = :to_id ";
		UserAttention uatt = (UserAttention) dataDao.getFirstObjectViaParam(hql, new String[] { "from_id", "to_id" },
				fid, tid);
		if (uatt != null) {
			dataDao.deleteObject(uatt);
		}
	}

	/**
	 * 
	 * @Description 获得两者的关系
	 * @param fid
	 * @param tid
	 * @return
	 */
	public UserAttention checkattention(long fid, long tid) {
		String hql = " from UserAttention where from_id = :from_id and to_id = :to_id ";
		UserAttention uatt = (UserAttention) dataDao.getFirstObjectViaParam(hql, new String[] { "from_id", "to_id" },
				fid, tid);
		return uatt;
	}

	/**
	 * 
	 * @Description 修改记录
	 * @param usera
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void chage(UserAttention usera) {
		dataDao.updateObject(usera);
	}

	/**
	 * 
	 * @Description 获得充值记录
	 * @param pageIndex
	 * @param pageSize
	 * @param mobile
	 * @param start_time
	 * @param end_time
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public PageBodyModel<ChargeRecord> getchargeRecord(Integer pageIndex, Integer pageSize, String mobile,
			String start_time, String end_time) {

		String wherehql = "where charge_num > 0 ";
		if (!CommonUtils.isEmptyString(mobile)) {
			wherehql += " and user_id = (select id from UserInfo where mobile = '" + mobile + "')";
		}

		if (!CommonUtils.isEmptyString(start_time)) {
			wherehql += " and '" + start_time + "' < charge_time and charge_time < '" + end_time + "'";
		}

		String orderhql = " order by charge_time desc";

		String chql = " select count(c) from ChargeRecord c " + wherehql;

		long count = (long) dataDao.getFirstObjectViaParam(chql, null);
		PageBodyModel<ChargeRecord> result = new PageBodyModel<ChargeRecord>();
		if (count > 0) {
			PageModel page = PageModel.createPage(count, pageIndex, pageSize);
			List<ChargeRecord> records = new ArrayList<ChargeRecord>();
			String hql = " from ChargeRecord " + wherehql + orderhql;
			records = (List<ChargeRecord>) dataDao.pageQueryViaParam(hql, pageSize, page.getPageIndex(), null);

			// 添加用户信息
			if (records != null && records.size() > 0) {
				for (ChargeRecord chargeRecord : records) {
					UserInfo info = dataDao.getObjectById(UserInfo.class, chargeRecord.getUser_id());
					if (info != null) {
						chargeRecord.setUser(info);
					}
				}
			}

			result = new PageBodyModel<ChargeRecord>(page, records);
		}
		return result;
	}

	/**
	 * 
	 * @Description 获取提现记录
	 * @param pageIndex
	 * @param pageSize
	 * @param mobile
	 * @param start_time
	 * @param end_time
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public PageBodyModel<ChargeRecord> getexchangeRecord(Integer pageIndex, Integer pageSize, String mobile,
			String start_time, String end_time) {
		String wherehql = "where 1 = 1 ";
		if (!CommonUtils.isEmptyString(mobile)) {
			wherehql += " and user_id = (select id from UserInfo where mobile = '" + mobile + "')";
		}

		if (!CommonUtils.isEmptyString(start_time)) {
			wherehql += " and '" + start_time + "' < charge_time and charge_time < '" + end_time + "'";
		}

		String orderhql = " order by charge_time desc";

		String chql = " select count(c) from ChargeRecord c " + wherehql;

		long count = (long) dataDao.getFirstObjectViaParam(chql, null);
		PageBodyModel<ChargeRecord> result = new PageBodyModel<ChargeRecord>();
		if (count > 0) {
			PageModel page = PageModel.createPage(count, pageIndex, pageSize);
			List<ChargeRecord> records = new ArrayList<ChargeRecord>();
			String hql = " from ChargeRecord " + wherehql + orderhql;
			//System.out.println(hql);
			records = (List<ChargeRecord>) dataDao.pageQueryViaParam(hql, pageSize, page.getPageIndex(), null);

			// 添加用户信息
			if (records != null && records.size() > 0) {
				for (ChargeRecord chargeRecord : records) {
					UserInfo info = dataDao.getObjectById(UserInfo.class, chargeRecord.getUser_id());
					if (info != null) {
						chargeRecord.setUser(info);
					}
				}
			}

			result = new PageBodyModel<ChargeRecord>(page, records);
		}
		return result;
	}

	/**
	 * 
	 * @Description 获取用户关注列表
	 * @param from_id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<UserInfo> getAttentionListByUser(long from_id) {
		String hql = "from UserInfo where id in(select to_id from UserAttention where from_id = :from_id order by attention_time desc)";
		List<UserInfo> list = (List<UserInfo>) dataDao.getObjectsViaParam(hql, new String[] { "from_id" }, from_id);
		String fhql = " from UserFavorability where user_id = :user_id and relation_id = :relation_id";
		String ahql = "from UserAttention where from_id = :from_id and to_id = :to_id";
		if (list != null && list.size() > 0) {
			for (UserInfo userInfo : list) {
				UserFavorability info = (UserFavorability) dataDao.getFirstObjectViaParam(fhql,
						new String[] { "user_id", "relation_id" }, from_id, userInfo.getId());
				if (info != null) {
					userInfo.setFavorability(info.getFavorability());
				}
				UserAttention attention = (UserAttention) dataDao.getFirstObjectViaParam(ahql,
						new String[] { "from_id", "to_id" }, from_id, userInfo.getId());
				if (attention != null) {
					userInfo.setRelation(attention.getRelation());
				}
			}
		}
		
		return list;
	}

	/**
	 * 获取粉丝列表
	 * @param to_id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<UserInfo> getfansListByUser(long to_id) {
		String hql = "from UserInfo where id in(select from_id from UserAttention where to_id = :to_id)";
		List<UserInfo> list = (List<UserInfo>) dataDao.getObjectsViaParam(hql, new String[] { "to_id" }, to_id);
		String fhql = " from UserFavorability where user_id = :user_id and relation_id = :relation_id";
		String ahql = "from UserAttention where from_id = :from_id and to_id = :to_id";
		if (list != null && list.size() > 0) {
			for (UserInfo userInfo : list) {
				UserFavorability info = (UserFavorability) dataDao.getFirstObjectViaParam(fhql,
						new String[] { "user_id", "relation_id" }, to_id, userInfo.getId());
				if (info != null) {
					userInfo.setFavorability(info.getFavorability());
				}
				UserAttention attention = (UserAttention) dataDao.getFirstObjectViaParam(ahql,
						new String[] { "from_id", "to_id" }, to_id, userInfo.getId());
				if (attention != null) {
					userInfo.setRelation(attention.getRelation());
				}
			}
		}
		
		return list;
	}

	/**
	 * 获取黑名单
	 * @param user_id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<UserInfo> getblackListByUser(long user_id) {
		String hql = "from UserInfo where id in(select to_id from BlackList where user_id = :user_id)";
		List<UserInfo> list = (List<UserInfo>) dataDao.getObjectsViaParam(hql, new String[] { "user_id" }, user_id);
		return list;
	}

	/**
	 * 举报
	 * @param report
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void accuse(ReportInfo report) {
		dataDao.addObject(report);
	}

	/**
	 * 添加黑名单
	 * @param black
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void addtoblack(BlackList black) {
		dataDao.addObject(black);
	}

	/**
	 * 移出黑名单
	 * @param userid
	 * @param blackid
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void removefromblack(long userid, long blackid) {
		String hql = "from BlackList where user_id = :user_id and to_id = :to_id";
		BlackList black = (BlackList) dataDao.getFirstObjectViaParam(hql, new String[] { "user_id","to_id" }, userid,blackid);
		if (black!=null) {
			dataDao.deleteObject(black);
		}
	}

	/**
	 * 获取用户黑名单列表
	 * @param userid
	 * @param blackid
	 * @return
	 */
	public BlackList getbalck(long userid, long blackid) {
		String hql = "from BlackList where user_id = :user_id and to_id = :to_id";
		BlackList black = (BlackList) dataDao.getFirstObjectViaParam(hql, new String[] { "user_id","to_id" }, userid,blackid);
		return black;
	}

	@SuppressWarnings("unchecked")
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void removeattention(long userid, long blackid) {
		String hql = "from UserAttention where (to_id = :to_id and from_id = :from_id) or (to_id = :from_id and from_id = :to_id)";
		List<UserAttention> list = (List<UserAttention>) dataDao.getObjectsViaParam(hql, new String[]{"to_id","from_id"},userid,blackid);
		if (list!=null) {
			for (UserAttention userAttention : list) {
				dataDao.deleteObject(userAttention);
			}
		}
	}
}
